<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数学值计算器</title>
    <link rel="stylesheet" href="https://www.unpkg.com/ew-message/dist/ew-message.min.css">
    <style>
        /* 全局样式 */
        body {
            font-family: "Arial", sans-serif;
            background-color: #f4f7f6;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #ffffff;
            border-radius: 10px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
            padding: 30px;
            width: 320px;
            text-align: center;
        }

        h1 {
            color: #4CAF50;
            font-size: 24px;
            margin-bottom: 20px;
        }

        .input-area {
            margin-bottom: 20px;
        }

        label {
            font-size: 16px;
            color: #555;
            margin-bottom: 10px;
            display: block;
        }

        input {
            width: 100%;
            padding: 12px;
            font-size: 14px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-bottom: 20px;
            box-sizing: border-box;
        }

        button {
            width: 100%;
            padding: 12px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #45a049;
        }

        .results {
            display: none;
            margin-top: 20px;
            padding: 10px;
            border-top: 1px solid #ccc;
            background-color: #f9f9f9;
            text-align: left;
        }

        .results p {
            margin: 10px 0;
            font-size: 14px;
        }

        .results span {
            font-weight: bold;
            color: #333;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>数学值计算器</h1>

        <div class="input-area">
            <label for="numbers-input">请输入数字（以逗号分隔）:</label>
            <input type="text" id="numbers-input" placeholder="例如：1, 2, 3, 4">
        </div>

        <button onclick="calculateValues()">计算</button>

        <div id="results" class="results">
            <p><strong>总和:</strong> <span id="sum">0</span></p>
            <p><strong>最大值:</strong> <span id="max">0</span></p>
            <p><strong>最小值:</strong> <span id="min">0</span></p>
            <p><strong>平均值:</strong> <span id="average">0</span></p>
        </div>
    </div>

    <script src="https://www.unpkg.com/ew-message/dist/ew-message.min.js"></script>

    <script>
        // 数学值计算函数
        const getMathValue = (...args) => {
            args = args.reduce((res, item) => {
                const n = Number(item);
                if (typeof n === 'number' && !isNaN(n)) res.push(n);
                return res;
            }, []);

            var result = {
                sum: 0,
                max: 0,
                min: 0,
                average: 0
            };

            const len = args.length;

            if (len === 0) return result; // 如果没有有效的输入，返回初始结果

            result['max'] = args[0];
            result['min'] = args[0];

            for (let i = 0; i < len; i++) {
                result['sum'] += args[i];
                if (args[i] > result['max']) result['max'] = args[i];
                if (args[i] < result['min']) result['min'] = args[i];
            }

            result['average'] = result['sum'] / len;
            return result;
        };

        // 计算并展示结果
        function calculateValues() {
            const input = document.getElementById('numbers-input').value;
            if (!`${input}`.trim()) {
                return ewMessage.warning('请输入数字!');
            }
            const numbers = input.split(',').map(num => num.trim());


            // 调用 getMathValue 函数计算结果
            const result = getMathValue(...numbers);

            // 显示结果
            document.getElementById('sum').textContent = result.sum;
            document.getElementById('max').textContent = result.max;
            document.getElementById('min').textContent = result.min;
            document.getElementById('average').textContent = result.average;

            // 显示结果区域
            document.getElementById('results').style.display = 'block';
        }
    </script>

</body>

</html>